﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Collections;

namespace yncTechWeb.Controls.DropDowns
{
    public class DropDownListBase : DropDownList
    {
        bool showSelectedItem = true;
        const string RequrieText = "&nbsp;<font color='Red'>*</font>";
        public DropDownListBase()
            : base()
        {
        }

        /// <summary>
        /// 是否添加请选择项, default:True
        /// </summary>
        public bool ShowSelectedItem
        {
            get
            {
                return showSelectedItem;
            }
            set
            {
                showSelectedItem = value;
            }
        }

        public Decimal DecimalValue
        {
            get
            {
                if (SelectedValue != null && !string.IsNullOrEmpty(SelectedValue))
                {
                    return decimal.Parse(SelectedValue);
                }
                return -1;
            }
        }
        public int IntValue
        {
            get
            {
                if (SelectedValue != null && !string.IsNullOrEmpty(SelectedValue))
                {
                    return int.Parse(SelectedValue);
                }
                return -1;
            }
        }

        public bool Required
        {
            get;
            set;
        }

        protected override void OnPreRender(EventArgs e)
        {

            //必填验证
            if (Required)
            {
                this.Attributes.Add("data-val", "true"); 
                this.Attributes.Add("data-val-required", "必填字段。");
            }
            base.OnPreRender(e);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            if (Required)
            {
                writer.Write(RequrieText);
            }
        }
    }
    #region  初始化系统类别
    public class DrpSystemTypes : DropDownListBase
    {
        public DrpSystemTypes()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            TSSystemCollection systems = new TSSystemCollection();
            systems.LoadAll();
            this.DataSource = systems;
            this.DataTextField = TSSystemMetadata.ColumnNames.SystemName;
            this.DataValueField = TSSystemMetadata.ColumnNames.SystemId;
            this.DataBind();
            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择系统类别", ""));
            }
        }
    }
    #endregion

    #region 频道列表
    public class DrpChannels : DropDownListBase
    {
        public DrpChannels()
        {
            SystemID = -1;
        }

        public decimal SystemID { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            TSFunctionsCollection functions = new TSFunctionsCollection();
            ArrayList list = new ArrayList();
            if (SystemID > 0)
            {
                list.Add(functions.Query.SystemId == SystemID);
            }
            list.Add(functions.Query.ParentID < 0);
            functions.Query.Where(list.ToArray());
            functions.Query.Load();

            this.DataSource = functions;
            this.DataTextField = TSFunctionsMetadata.ColumnNames.FunctionName;
            this.DataValueField = TSFunctionsMetadata.ColumnNames.FunctionID;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择频道", ""));

            }

        }
    }
    #endregion

    #region 用户角色
    public class DrpRoles : DropDownListBase
    {
        public DrpRoles()
        {
            ShowInActive = false;
        }

        public bool ShowInActive
        {
            get;
            set;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            TSRoleCollection roles = new TSRoleCollection();
            if (!ShowInActive)
            {
                roles.Query.Where(roles.Query.IsActive == true);
            }
            roles.Query.Load();
            this.DataSource = roles;
            this.DataTextField = TSRoleMetadata.ColumnNames.RoleName;
            this.DataValueField = TSRoleMetadata.ColumnNames.RoleID;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择角色", ""));
            }
        }
    }
    #endregion

    #region 用户
    public class DrpUsers : DropDownListBase
    {
        public DrpUsers()
        {
            ShowInActive = false;
        }

        public bool ShowInActive
        {
            get;
            set;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            TSUsersQuery q1 = new TSUsersQuery("q1");
            TSUserInfoQuery q2 = new TSUserInfoQuery("q2");
            q1.Select(q1.UserID, q2.UserName);
            q1.InnerJoin(q2).On(q1.UserID==q2.UserID);

            if (!ShowInActive)
            {
                q1.Where(q1.IsActive==true);
            }
            q1.OrderBy(q2.UserName.Ascending);
            this.DataSource = q1.LoadDataTable();
            this.DataTextField = TSUserInfoMetadata.ColumnNames.UserName;
            this.DataValueField = TSUserInfoMetadata.ColumnNames.UserName;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择用户", ""));
            }
        }
    }
    #endregion

    #region 用户Value是ID
    public class DrpUsersId : DropDownListBase
    {
        public DrpUsersId()
        {
            ShowInActive = false;
        }

        public bool ShowInActive
        {
            get;
            set;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            TSUsersQuery q1 = new TSUsersQuery("q1");
            TSUserInfoQuery q2 = new TSUserInfoQuery("q2");
            q1.Select(q1.UserID, q2.UserName);
            q1.InnerJoin(q2).On(q1.UserID == q2.UserID);

            if (!ShowInActive)
            {
                q1.Where(q1.IsActive == true);
            }
            q1.OrderBy(q2.UserName.Ascending);
            this.DataSource = q1.LoadDataTable();
            this.DataTextField = TSUserInfoMetadata.ColumnNames.UserName;
            this.DataValueField = TSUserInfoMetadata.ColumnNames.UserID;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择用户", ""));
            }
        }
    }
    #endregion
    
    #region 用户Value是LoginName
    public class DrpUsersLoginName : DropDownListBase
    {
        public DrpUsersLoginName()
        {
            ShowInActive = false;
        }

        public bool ShowInActive
        {
            get;
            set;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            TSUsersQuery q1 = new TSUsersQuery("q1");
            TSUserInfoQuery q2 = new TSUserInfoQuery("q2");
            q1.Select(q1.LoginName, q2.UserName);
            q1.InnerJoin(q2).On(q1.UserID == q2.UserID);

            if (!ShowInActive)
            {
                q1.Where(q1.IsActive == true);
            }
            q1.OrderBy(q2.UserName.Ascending);
            this.DataSource = q1.LoadDataTable();
            this.DataTextField = TSUserInfoMetadata.ColumnNames.UserName;
            this.DataValueField = TSUsersMetadata.ColumnNames.LoginName;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择用户", ""));
            }
        }
    }
    #endregion

    #region 基本字典
    public class DrpCodeTable : DropDownListBase
    {
        public DrpCodeTable()
        {
        }

        public string Category { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            TSCodeTableCollection codes = new TSCodeTableCollection();
            ArrayList list = new ArrayList();
            if (!string.IsNullOrEmpty(Category))
            {
                list.Add(codes.Query.CodeCategory == Category);
            }
            codes.Query.Where(list.ToArray()).OrderBy(codes.Query.CodeSequence.Ascending);
            codes.Query.Load();

            this.DataSource = codes;
            this.DataTextField = TSCodeTableMetadata.ColumnNames.Value1;
            this.DataValueField = TSCodeTableMetadata.ColumnNames.Code;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));

            }

        }
    }
    public class DrpCodeTableValid : DropDownListBase
    {
        public DrpCodeTableValid()
        {
        }

        public string Category { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            TSCodeTableCollection codes = new TSCodeTableCollection();
            ArrayList list = new ArrayList();
            if (!string.IsNullOrEmpty(Category))
            {
                list.Add(codes.Query.CodeCategory == Category);
            }
            list.Add(codes.Query.EffectiveEndDate>= DateTime.Now);
            codes.Query.Where(list.ToArray()).OrderBy(codes.Query.CodeSequence.Ascending);
            codes.Query.Load();

            this.DataSource = codes;
            this.DataTextField = TSCodeTableMetadata.ColumnNames.Value1;
            this.DataValueField = TSCodeTableMetadata.ColumnNames.Code;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));

            }

        }
    }
    #endregion

    #region 凭证存放机构
    public class DrpThirdUnit : DropDownListBase
    {
        public DrpThirdUnit()
        {
            ShowInActive = false;
        }

        public bool ShowInActive
        {
            get;
            set;
        }

        public string ThirdType
        {
            get;
            set;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            BFThirdUnitCollection tunit = new BFThirdUnitCollection();
            ArrayList arg = new ArrayList();
            ArrayList argOr = new ArrayList();
            if (!ShowInActive)
            {
                arg.Add(tunit.Query.Status == (int)ActiveStatus.Active);         
            }
            if (!string.IsNullOrEmpty(ThirdType))
            {
                string[] types = ThirdType.Split(',');
                if (types.Length == 1)
                {
                    arg.Add(tunit.Query.ThirdType == types[0]);
                }
                else
                {
                    if (types.Length > 1)
                    {
                        for (int i = 0; i < types.Length; i++)
                        {
                            argOr.Add(tunit.Query.ThirdType == types[i]);
                        }
                        arg.Add(tunit.Query.Or(argOr.ToArray()));
                    }
                }
            }

            if (arg.Count > 0)
            {
                tunit.Query.Select(tunit.Query.ThirdUnitID, tunit.Query.Name).Where(arg.ToArray());
            }
            tunit.Query.Load();
            this.DataSource = tunit;
            this.DataTextField = BFThirdUnitMetadata.ColumnNames.Name;
            this.DataValueField = BFThirdUnitMetadata.ColumnNames.ThirdUnitID;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));
            }
        }
    }
    #endregion

    #region 凭证存放机构
    public class DrpThirdUnitName : DropDownListBase
    {
        public DrpThirdUnitName()
        {
            ShowInActive = false;
        }

        public bool ShowInActive
        {
            get;
            set;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            BFThirdUnitCollection bFThirdUnitCollection = new BFThirdUnitCollection();
            bFThirdUnitCollection.Query.Select(bFThirdUnitCollection.Query.Name);
            if (!ShowInActive)
            {
                bFThirdUnitCollection.Query.Where(bFThirdUnitCollection.Query.Status == (int)ActiveStatus.Active);
            }
            bFThirdUnitCollection.Query.Load();
            this.DataSource = bFThirdUnitCollection;
            this.DataTextField = BFThirdUnitMetadata.ColumnNames.Name;
            this.DataValueField = BFThirdUnitMetadata.ColumnNames.ThirdUnitID;
            this.DataBind();

            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));
            }
        }
    }
    #endregion

    public class DrpCompany : DropDownListBase
    {
        public DrpCompany()
        { }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            BFCompanyDicQuery q1 = new BFCompanyDicQuery();
            q1.Select(q1.CompanyNo, (q1.CompanyName + "(" + q1.CompanyCode + ")").As("Company"))
                .OrderBy(q1.CompanyName.Ascending,q1.CompanyCode.Ascending);
            this.DataSource = q1.LoadDataTable();
            DataTextField = "Company";
            DataValueField = "Company";

            this.DataBind();


            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));
            }
            
        }
    }

    public class DrpCompanyName : DropDownListBase
    {
        public DrpCompanyName()
        { }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            BFCompanyDicQuery q1 = new BFCompanyDicQuery();
            q1.Select(q1.CompanyName).OrderBy(q1.CompanyName.Ascending);
            q1.es.Distinct = true;
            this.DataSource = q1.LoadDataTable();
            DataTextField = "CompanyName";
            DataValueField = "CompanyName";

            this.DataBind();


            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));
            }

        }
    }

    public class DrpCompanyCode : DropDownListBase
    {
        public DrpCompanyCode()
        { }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            BFCompanyDicQuery q1 = new BFCompanyDicQuery();
            q1.Select(q1.CompanyCode).OrderBy(q1.CompanyCode.Ascending);
            q1.es.Distinct = true;
            this.DataSource = q1.LoadDataTable();
            DataTextField = "CompanyCode";
            DataValueField = "CompanyCode";

            this.DataBind();


            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));
            }

        }
    }

    public class DrpCompanyNo : DropDownListBase
    {
        public DrpCompanyNo()
        { }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            BFCompanyDicQuery q1 = new BFCompanyDicQuery();
            q1.Select(q1.CompanyNo).OrderBy(q1.CompanyNo.Ascending);
            q1.es.Distinct = true;
            this.DataSource = q1.LoadDataTable();
            DataTextField = "CompanyNo";
            DataValueField = "CompanyNo";

            this.DataBind();


            if (ShowSelectedItem)
            {
                this.Items.Insert(0, new ListItem("请选择...", ""));
            }

        }
    }



    public class DrpYear : DropDownListBase
    {
        public DrpYear()
        { }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            int start = DateTime.Now.Year;
            for (int i = start; i >=1986; i--)
            {
                this.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            this.Items.Insert(0, new ListItem("请选择...", ""));

        }
    }

    public class DrpMonth : DropDownListBase
    {
        public DrpMonth()
        { }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            for (int i = 1; i <=12; i++)
            {
                if (i < 10)
                {
                    this.Items.Add(new ListItem(i.ToString(), "0"+i.ToString()));
                }
                else
                {
                    this.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
            }
            this.Items.Insert(0, new ListItem("请选择...", ""));

        }
    }

    public class DrpDay : DropDownListBase
    {
        public DrpDay()
        { }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            for (int i = 1; i <= 31; i++)
            {
                if (i < 10)
                {
                    this.Items.Add(new ListItem(i.ToString(), "0" + i.ToString()));
                }
                else
                {
                    this.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
            }
            this.Items.Insert(0, new ListItem("请选择...", ""));

        }
    }
}